package com.fire.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fire.bo.collage.req.CollageGoodListReq;
import com.fire.bo.collage.req.CollageGoodReq;
import com.fire.bo.collage.req.GofireNameReq;
import com.fire.bo.collage.req.GofireProductReq;
import com.fire.bo.collage.req.GofireReq;
import com.fire.bo.collage.res.CollageGoodListRes;
import com.fire.bo.collage.res.GofireProductRes;
import com.fire.bo.collage.res.GofireRes;
import com.fire.bo.coupon.req.DelCouponReq;
import com.fire.common.RspObjBean;
import com.fire.service.CollageGoodService;
import com.fire.util.BussinessUtils;
import com.fire.util.ErrRsp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Controller
@CrossOrigin
@RequestMapping("/pc/collage")
@Api(value = "发起拼单管理", description = "发起拼单管理")
public class CollageController
{
	@Autowired
	private CollageGoodService collageGoodService;
	
	/**
	 * 设置发起拼单数量
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "设置发起拼单数量", notes = "设置发起拼单数量")
	@RequestMapping(value = "/addCollageGoodNum", method = RequestMethod.POST)
	public @ResponseBody RspObjBean addCollageGoodNum(
			@RequestBody @Valid CollageGoodReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			Map<String, Object> map = new HashMap<>();
			
			map.put("goodId", bean.getGoodId());
			int num = collageGoodService.collageCount(map);
			
			if(num > Integer.parseInt(bean.getCollageNum())) {
				collageGoodService.addCollageGoodNum(bean);
				// 根据上级类别key得到类别集合
				return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
						ErrRsp.SUCCESS_CODE_DESC, null);
			}else {
				return BussinessUtils.rspErrorHeadInfo(ErrRsp.ERROR_CODE,
						ErrRsp.ERROR_CODE_DESC, null);
			} 
			
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	/**
	 * 设置拼单数量列表
	 * 
	 * @param reqPersonEntryDateBean
	 * @param bindingResult
	 * @return
	 * @version:v1.0
	 * @author:ZhangPeng
	 * @date:2017年06月02日 上午09:07:14
	 */
	@ApiOperation(value = "设置拼单数量列表", notes = "设置拼单数量列表",response = CollageGoodListRes.class)
	@RequestMapping(value = "/findGoodList", method = RequestMethod.POST)
	public @ResponseBody RspObjBean findGoodList(
			@RequestBody @Valid CollageGoodListReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			List<CollageGoodListRes> list = collageGoodService.findGoodList(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, list,collageGoodService.findGoodListCount(bean));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "新增商品", notes = "新增商品")
	@RequestMapping(value = "/addGofireProject", method = RequestMethod.POST)
	public @ResponseBody RspObjBean addGofireProject(
			@RequestBody @Valid GofireProductRes bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			collageGoodService.addGofireProject(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "修改商品", notes = "修改商品")
	@RequestMapping(value = "/updateGofireProject", method = RequestMethod.POST)
	public @ResponseBody RspObjBean updateGofireProject(
			@RequestBody @Valid GofireProductRes bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			collageGoodService.updateGofireProject(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "列表商品", notes = "列表商品",response = GofireProductRes.class)
	@RequestMapping(value = "/gofireProjectList", method = RequestMethod.POST)
	public @ResponseBody RspObjBean gofireProjectList(
			@RequestBody @Valid GofireProductReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			List<GofireProductRes> list = collageGoodService.gofireProjectList(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, list,collageGoodService.gofireProjectListCount(bean));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "单个商品", notes = "单个商品",response = GofireProductRes.class)
	@RequestMapping(value = "/gofireProjectById", method = RequestMethod.POST)
	public @ResponseBody RspObjBean gofireProjectById(
			@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			GofireProductRes info = collageGoodService.gofireProjectById(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, info);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "删除商品", notes = "删除商品")
	@RequestMapping(value = "/delgofireProject", method = RequestMethod.POST)
	public @ResponseBody RspObjBean delgofireProject(
			@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			collageGoodService.delgofireProject(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "用户拼单列表", notes = "用户拼单列表",response = GofireRes.class)
	@RequestMapping(value = "/findAllFireRes", method = RequestMethod.POST)
	public @ResponseBody RspObjBean findAllFireRes(
			@RequestBody @Valid GofireNameReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			List<GofireRes> list = collageGoodService.findAllFireRes(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, list,collageGoodService.findAllFireResCount(bean));
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "查询单个用户拼单", notes = "查询单个用户拼单",response = GofireRes.class)
	@RequestMapping(value = "/findFireResInfo", method = RequestMethod.POST)
	public @ResponseBody RspObjBean findFireResInfo(
			@RequestBody @Valid DelCouponReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			GofireRes info = collageGoodService.findFireResInfo(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, info);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "新增商品", notes = "新增商品")
	@RequestMapping(value = "/addGoFire", method = RequestMethod.POST)
	public @ResponseBody RspObjBean addGoFire(
			@RequestBody @Valid GofireReq bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			collageGoodService.addGoFire(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	@ApiOperation(value = "修改商品", notes = "修改商品")
	@RequestMapping(value = "/updateGoFire", method = RequestMethod.POST)
	public @ResponseBody RspObjBean updateGoFire(
			@RequestBody @Valid GofireRes bean, BindingResult bindingResult)
	{
		// 非空验证
		if (bindingResult.hasErrors())
		{
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.PARAM_NULL_CODE,
					bindingResult.getFieldError().getDefaultMessage());
		}

		try
		{
			collageGoodService.updateGoFire(bean);
			// 根据上级类别key得到类别集合
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SUCCESS_CODE,
					ErrRsp.SUCCESS_CODE_DESC, null);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			return BussinessUtils.rspErrorHeadInfo(ErrRsp.SYSTEM_ERROR_CODE,
					ErrRsp.SYSTEM_ERROR_DESC);
		}
	}
	
	
	
}
